package com.eunion.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import com.eunion.web.cms.entity.University;

public class BaikeUtils {

    public static final String UNIVERSITY_CODE_A = "学校代码";
    public static final String UNIVERSITY_CODE_B = "院校代码";
    public static final String UNIVERSITY_NAME_BRIEF = "简 称";
    public static final String UNIVERSITY_NAME_ZH = "中文名";
    public static final String UNIVERSITY_NAME_EN_A = "英文名";
    public static final String UNIVERSITY_NAME_EN_B = "外文名";
    public static final String UNIVERSITY_IMPORT_SUBJECT = "国家重点学科";
    public static final String UNIVERSITY_ADDRESS_A = "学校地址";
    public static final String UNIVERSITY_ADDRESS_B = "地 址";
    public static final String UNIVERSITY_ADDRESS_C = "所属地区";
    public static final String UNIVERSITY_ADDRESS_D = "院校属地";
    public static final String UNIVERSITY_COLLEGE_SET = "院系设置";
    public static final String UNIVERSITY_TYPE_A = "学校类型";
    public static final String UNIVERSITY_TYPE_B = "类 型";
    public static final String UNIVERSITY_MOTTO = "校 训";
    public static final String UNIVERSITY_WEBSITE = "学校官网";
    public static final String UNIVERSITY_ANNIVERSARY = "校庆日";
    public static final String UNIVERSITY_LEADER = "现任领导";
    public static final String UNIVERSITY_BOHOU_POINTS = "博士后";
    public static final String UNIVERSITY_BO_POINTS = "博士点";
    public static final String UNIVERSITY_SHUO_POINTS = "硕士点";
    public static final String UNIVERSITY_LOGO_URL = "头像";
    public static final String UNIVERSITY_CREATION = "创办时间";
    public static final String UNIVERSITY_CLASSIFY = "类 别";
    public static final String UNIVERSITY_TAGS = "属 性";
    public static final String UNIVERSITY_BELONG = "主管部门";
    public static final String UNIVERSITY_CLASSMATE = "知名校友";

    public static University getUninversity(String name) {

        String url = "https://baike.baidu.com/item/";
        String response = HttpClient.doGet(url + name,"utf-8");
        Map<String,String> rqMap = getPqvalue(response);
        Iterator<String> keys = rqMap.keySet().iterator();
        University ret = new University();
        while(keys.hasNext()) {
            String key = keys.next();
            String value = rqMap.get(key);
            if (UNIVERSITY_CODE_A.equals(key) || UNIVERSITY_CODE_B.equals(key)) {
                //if (StringUtils.isBlank(ret.getCode())) {
                ret.setCode(value);
                //}
            }
            if (UNIVERSITY_NAME_BRIEF.equals(key)) {
                ret.setShortName(value);
            }
            if (UNIVERSITY_NAME_ZH.equals(key)) {
                ret.setName(value);
            }
            if (UNIVERSITY_NAME_EN_A.equals(key) || UNIVERSITY_NAME_EN_B.equals(key)) {
                ret.setEnName(value);
            }
            if (UNIVERSITY_IMPORT_SUBJECT.equals(key)) {
                ret.setImportSubject(value);
            }
            if (UNIVERSITY_ADDRESS_A.equals(key) ||
                    UNIVERSITY_ADDRESS_B.equals(key) ||
                    UNIVERSITY_ADDRESS_C.equals(key) ||
                    UNIVERSITY_ADDRESS_D.equals(key)) {
                ret.setAddress(value);
            }
            if (UNIVERSITY_COLLEGE_SET.equals(key)) {
                ret.setCollegeSet(value);
            }
            if (UNIVERSITY_TYPE_A.equals(key) || UNIVERSITY_TYPE_B.equals(key)) {
                ret.setType(value);
            }
            if (UNIVERSITY_MOTTO.equals(key)) {
                ret.setMotto(value);
            }
            if (UNIVERSITY_WEBSITE.equals(key)) {
                ret.setWebsite(value);
            }
            if (UNIVERSITY_ANNIVERSARY.equals(key)) {
                ret.setAnniversary(value);
            }
            if (UNIVERSITY_LEADER.equals(key)) {
                ret.setLeader(value);
            }
            if (UNIVERSITY_BOHOU_POINTS.equals(key)) {
                if (StringUtils.isNumeric(value)) {
                    ret.setBohouPoints(Integer.parseInt(value));
                }
            }
            if (UNIVERSITY_BO_POINTS.equals(key)) {
                if (StringUtils.isNumeric(value)) {
                    ret.setBoPoints(Integer.parseInt(value));
                }
            }
            if (UNIVERSITY_SHUO_POINTS.equals(key)) {
                if (StringUtils.isNumeric(value)) {
                    ret.setShuoPoints(Integer.parseInt(value));
                }
            }
            if (UNIVERSITY_LOGO_URL.equals(key)) {
                ret.setLogoUrl(value);
            }
            if (UNIVERSITY_CREATION.equals(key)) {
                ret.setCreation(value);
            }
            if (UNIVERSITY_CLASSIFY.equals(key)) {
                ret.setClassify(value);
            }
            if (UNIVERSITY_TAGS.equals(key)) {
                ret.setTags(value);
            }
            if (UNIVERSITY_BELONG.equals(key)) {
                ret.setBelong(value);
            }
            if (UNIVERSITY_CLASSMATE.equals(key)) {
                ret.setClassmate(value);
            }
        }
        return ret;
    }

    private static Map<String,String> getPqvalue(String ret){
        Document doc = Jsoup.parse(ret);
        Map<String,String> universitySx = new HashMap<>();

        Elements nameElemnts = doc.getElementsByClass("basicInfo-item name");
        Elements valueElemnts = doc.getElementsByClass("basicInfo-item value");

        if (nameElemnts != null && valueElemnts != null){
            List<String> nameStr  = new ArrayList<>();
            for(Element element : nameElemnts) {
                nameStr.add(element.text());
            }

            List<String> valueStr = new ArrayList<>();
            for(Element element : valueElemnts) {
                valueStr.add(element.text());
            }
            int size = nameStr.size()>valueStr.size()?valueStr.size():nameStr.size();
            for (int i=0;i<size;i++){
                //System.out.println(nameStr.get(i)+":" + valueStr.get(i));
                universitySx.put(nameStr.get(i),valueStr.get(i));
            }
        }
        // bottomLine 官网
        Elements bottomLineElemnts = doc.getElementsByClass(" bottomLine");
        if (bottomLineElemnts != null){
            for (Element bottomLine : bottomLineElemnts){
                String dt = bottomLine.getElementsByTag("dt").text();
                if ("学校官网".equals(dt)){
                    String gwUrl = bottomLine.getElementsByTag("a").text();
                    universitySx.put(dt,gwUrl);
                    break;
                }
            }
        }
        // 图集
        Elements summaryPicElemnts = doc.getElementsByClass("summary-pic");
        if (summaryPicElemnts != null){
            for (Element summaryPic: summaryPicElemnts){
                String summaryPicUrl = summaryPic.getElementsByTag("img").attr("src");
                universitySx.put("头像",summaryPicUrl);
                break;
            }
        }

        return universitySx;
    }
}
